<?php
namespace V1\Logic;


class StockLogic  {
      
     /**
     * 定时从库中取深证成指、上证指数、创业板指保存到redis中
     */
      public static function getMarketIndex($day) {
              $indexStockCodeList =['sh000001','sz399001','sz399006'];
              $stockIndexList =[];
              foreach ($indexStockCodeList as $stockcode) {
                $stockIndexList[] = StockLogic::getStockInfo($day,$stockcode);
              }
           return $stockIndexList;
      }
      /**
       * 获取股票实时数据
       * 当前股价、涨跌幅度、马蹄情绪、关注人数、涨跌数值、马蹄热度
       * @param type $stock_code
       */
      public static function getStockInfo($day,$stockcode) {
     
          $stockInfo = S($stockcode,'',array('prefix'=>'gamestock'));
         
          if(empty($stockInfo)){
              
                //$stockInfo = M('Stock'.str_replace('-', '', $day),C('RealTimeStockDb')['DB_PREFIX'],C('RealTimeStockDb')['DB_CONN'])->where("stock_code='{$stockcode}'")->order('id desc')->find();     
                //if(empty($stockInfo)) 
                $stockInfo = getRealTimeStockInfo ($stockcode);
                $result = M('StockInfo')->where("stockcode='{$stockcode}'")->field('stocklogo,industry,topics')->find();
                $stockInfo['industry'] = $result['industry'];
                $stockInfo['topics'] = !empty($result['topics'])? split(";", $result['topics'])[0] :"";
                $stockInfo['stock_logo'] = !empty($result['stocklogo'])? str_replace('/Uploads','', $result['stocklogo']) :"";
                //关注
                $stockInfo['follows'] = M('UserStock')->where("status=1 and stock_code='{$stockcode}'")->count();
                
                $stockInfo['prevTradeDate'] = CalendarLogic::getPreTradeDay($day);
                $stockInfo['curTradeDate'] = $day;
                
                //涨跌预测、热股排名
                $matiIndicators = StockLogic::getStockMatiIndicators($day,substr($stockcode, 2));
                
                $stockInfo = array_merge($stockInfo,$matiIndicators);

                S($stockcode,$stockInfo,array('prefix'=>'gamestock','expire'=>300));
          }
          return $stockInfo;
          
      }
      public static function getStockMatiIndicators($day,$stockcode){
        
        $matiIndicators = S($day.$stockcode,'',array('prefix'=>'stockMatiIndicators'));
           if(empty($matiIndicators)){
                //涨跌预测、热股排名
                $where=array(
                    'trading_day'=>$day,
                    'stock_type'=>'A',
                    'status'=>'0',
                    'stock_code'=> $stockcode,
                );
                $matiIndicators= M('MatiIndex')->where($where)->field('stock_order,s_index')->find();
                
                if(empty($matiIndicators)){
                    $matiIndicators = ['stock_order'=>0,'s_index'=>0];
                }
                S($day.$stockcode,$matiIndicators,array('prefix'=>'stockMatiIndicators','expire'=>3600));
           }
        return $matiIndicators;
      }
      /**
       * 股票是新股或者次新股
       * @param type $stock_code
       * @return boolean
       */
      public static function isNewOrSubNewStock($stock_code){
           //to-do
          return false;
      }
      /**
       * 股票指定交易日期前两个交易日持续涨停或跌停
       * @param type $day  date 如2015-05-25 指定交易日期
       * @param type $stock_code string 股票代码
       * @return type boolean true 前两个交易日是持续涨停或跌停 false 前两个交易日不是持续涨停或跌停
       */
      public static function isContinuousTwoDaysUpOrDown($day,$stock_code) {
          //to-do
          return false;
      }
      /**
       * 股票是否停牌中
       * @param type $day
       * @param type $stock_code
       * @return boolean
       */
      public static function isSuspended($stock_code) {
          //to-do
          return false;
      }
      /**
       * 股票游戏是否涨停或者跌停,如果是则更新当前游戏状态结束
       * @param type $day
       * @param type $stock_code
       * @return boolean
       */
      public static function isStop($game_id) {
          //to-do
          //判断是否涨停
          //如果是则更新当前游戏状态
          $gameModel = M('Game');
          $status = $gameModel->where("id={$game_id}")->getField('status');
          
          return $status == 0;
      }
      /**
       * 人工干预股票不参加游戏
       * @param type $day
       * @param type $stock_code
       * @return boolean
       */
      public static function manualInterveneNotJoinGame($day,$game_id) {
           //to-do mt_stock_base=1 或者 mt_game status=3 
          
          return false;
      }
      /**
       * 按涨或者跌获取主题或行业股票信息列表
       * @param type $sector_type  single topic industry
       * @return boolean
       */
      public function getSectorTopN($sector_type)
      {     
          $sectorTopNDataList = S($sector_type,'',array('prefix'=>"{$sector_type}sectortopn"));
           
          //缓存主题或行业数据
          $sectorDataList = S($sector_type,'',array('prefix'=>"{$sector_type}sector"));
          if(empty($sectorDataList)){
              
          }
          //缓存行业或主题对应的股票信息
          $stockDataList = S($sector_type,'',array('prefix'=>"{$sector_type}stock"));
          if(empty($stockDataList)){
              
          }
          
          return false;
      }
      /**
       * 按行业分股票信息列表
       * @return boolean
       */
       public function getStockListBySector()
      {
           
          return false;
      }
}